$(document).ready(function(){
	$('.ads').each(function(i, e){
		$(e).append('<button class="edit-banner-inline">Edit</button>');
	});

	var $edit_area = $(".adv-edit-inline");
	$('.edit-banner-inline').livequery('click', function(){
		$edit_area.hide();
		var $adv = $(this).parent('div.ads').attr('id');
		var $button = $(this);
		if ($adv)
		{
			$edit_area.css({top: $button.offset().top + 30, left: $button.offset().left - 100});
			$.post(tpl_url + '/php/ads-edit-inline.php', {group: $adv, action: 'get-adv-here'}, function(data){
				if (data.empty_adv == '1')
				{
					$('input[name="adv-inline-id"]').val('new-insert');
					$('input[name="adv_group"]').val(data.adv_group);
					$('textarea[name="adv-inline-text"]').val('');
				}
				else
				{
					$('input[name="adv-inline-id"]').val(data.id);
					$('textarea[name="adv-inline-text"]').val(data.text);
				}
				$edit_area.fadeIn();
			}, 'json');
		}
		else
		{
			alert('This zone isn\'t available.');
		}
		return false;
	});
	$('#save-adv-inline').livequery('click', function(){
		var form = $(this).parent('div.adv-edit-inline');
	
		var $id = form.find('input[name="adv-inline-id"]').val();
		var $text = form.find('textarea[name="adv-inline-text"]').val();
		var $adv_group = form.find('input[name="adv_group"]').val();
		
		$.post(tpl_url + '/php/ads-edit-inline.php', {id: $id, text: $text, adv_group: $adv_group, action: 'save-adv'}, function(){
			$('.adv-edit-inline-saved').css({visibility: 'visible'});
			setTimeout(function(){ $('.adv-edit-inline-saved').css({visibility: 'hidden'}); }, 3000);
//			alert($id + ' - save function - ' +  $text);
		})
		
		
	});
	$('.adv-edit-inline-cancel').livequery('click', function(){
		$('.adv-edit-inline').hide();
		return false;
	});
});